/*
 * @Author: 张贤石 “阿册011432181@163.com”
 * @Date: 2022-08-14 18:34:49
 * @LastEditors: 张贤石 “阿册011432181@163.com”
 * @LastEditTime: 2022-08-14 18:53:47
 * @FilePath: \mall-management\src\hooks\components\useTable.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { reactive, ref, watch } from "vue";

export function useTable<T, R extends IgetListRes<any>>(
  requestApi: (data: T) => IPR<R>,
  params: T
) {
  const pageination = reactive({
    total: 0,
    currentPage: 1,
    pageSize: 10,
    small: false,
    background: false,
    disabled: false,
  });

  let TableList = ref<R["list"]>([]);
  async function get() {
    let res = await requestApi({
      pageNum: pageination.currentPage,
      pageSize: pageination.pageSize,
      ...params,
    });

    if (res.code !== 200) return;
    TableList.value = res.data.list;
    pageination.total = res.data.total;
  }
  get();

  const handleSizeChange = () => {
    get();
  };
  const handleCurrentChange = () => {
    get();
  };
  // 搜索 好东西
  const seachKey = (paramsS: T) => {
    params = {} as T;
    for (let k in paramsS) {
      params[k] = paramsS[k];
    }
    pageination.currentPage = 1;
    get();
  };
  return {
    seachKey,
    pageination,
    TableList,
    handleSizeChange,
    handleCurrentChange,
    get,
  };
}
